package org.OpenGeoPortal.Download;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

abstract class ConfigRetriever {
	public String configFilePath;
	public JSONObject configContents = null;
	
	public void setConfigFilePath(String configFilePath){
		this.configFilePath = configFilePath;
	}
	
	public String loadConfigFile() throws java.io.IOException {
	    byte[] buffer = new byte[(int) new File(this.configFilePath).length()];
	    BufferedInputStream bufferedInputStream = null;
	    try {
	        bufferedInputStream = new BufferedInputStream(new FileInputStream(this.configFilePath));
	        bufferedInputStream.read(buffer);
	    } finally {
	        if (bufferedInputStream != null) try { bufferedInputStream.close(); } catch (IOException ignored) { }
	    }
	    return new String(buffer);
	}
	
	public void readConfigFile() throws ParseException, IOException{
		if (configContents == null){
			//use simple json to parse the file and place into a hashmap 
			JSONParser parser = new JSONParser();
			JSONObject jsonObj = (JSONObject) parser.parse(this.loadConfigFile());
			JSONObject jsonResponse = (JSONObject) jsonObj.get("config");
			this.configContents = jsonResponse;
		}
	}

}
